package com.sun;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.cn/problems/4sum/
 * 给你一个由 n 个整数组成的数组nums ，和一个目标值 target 。
 * 请你找出并返回满足下述全部条件且不重复的四元组[nums[a], nums[b], nums[c], nums[d]]
 * （若两个四元组元素一一对应，则认为两个四元组重复）：
 */
public class Test18四数之和 {
    public static void main(String[] args) {
        int[] nums = {1,0,-1,0,-2,2};
        int[] nums1 = {2,2,2,2,2};
        int[] nums2 = {1000000000,1000000000,1000000000,1000000000};
        int i = nums2[0] + nums2[1] + nums2[2] + nums2[3];
        System.out.println(i);
        System.out.println(Integer.MAX_VALUE);
        List<List<Integer>> lists = fourSum1(nums2, -294967296);
        lists.forEach(System.out::println);

    }

    public static List<List<Integer>> fourSum1(int[] nums, int target) {
        int n = nums.length;
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int first = 0; first < n - 1; first++) {
            if (first > 0 && nums[first] == nums[first-1]){
                continue;
            }
            for (int second = first+1; second < n - 1; second++) {
                if(second > first +1 && nums[second] == nums[second-1]){
                    continue;
                }
                int third = second + 1;
                int fourth = n - 1;
                while (fourth > third){
                    if ((long)nums[first] + nums[second] + nums[third] + nums[fourth] == target){
                        List<Integer> temp = new ArrayList<>();
                        temp.add(nums[first]);
                        temp.add(nums[second]);
                        temp.add(nums[third]);
                        temp.add(nums[fourth]);
                        res.add(temp);
                        third++;
                        while (nums[third] == nums[third -1]){
                            if(third == fourth){
                                break;
                            }
                            third++;
                        }
                    }else if ((long)nums[first] + nums[second] + nums[third] + nums[fourth] > target){
                        fourth --;
                        while (third < fourth && nums[fourth] == nums[fourth+1]){
                            fourth--;
                        }
                    }else {
                        third++;
                        while (third < fourth && nums[third] == nums[third -1]){
                            third++;
                        }
                    }
                }
            }
        }
        return res;
    }

    /**
     *方法2：力扣官方
     */
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> quadruplets = new ArrayList<List<Integer>>();
        if (nums == null || nums.length < 4) {
            return quadruplets;
        }
        Arrays.sort(nums);
        int length = nums.length;
        for (int i = 0; i < length - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
                break;
            }
            if ((long) nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target) {
                continue;
            }
            for (int j = i + 1; j < length - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                if ((long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) {
                    break;
                }
                if ((long) nums[i] + nums[j] + nums[length - 2] + nums[length - 1] < target) {
                    continue;
                }
                int left = j + 1, right = length - 1;
                while (left < right) {
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum == target) {
                        quadruplets.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        while (left < right && nums[left] == nums[left + 1]) {
                            left++;
                        }
                        left++;
                        while (left < right && nums[right] == nums[right - 1]) {
                            right--;
                        }
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        right--;
                    }
                }
            }
        }
        return quadruplets;
    }


}
